/*
** check_events.c for  in /home/gomes_m//rattrapage/server
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Tue Jul 23 12:49:20 2013 mickael gomes
** Last update Wed Jul 24 14:03:27 2013 mickael gomes
*/

#include		<stdio.h>
#include		"struct.h"
#include		"prototypes.h"

void			check_new_client(t_socket *socket, t_list *list)
{
  int			fd;

  fd = xaccept(socket);
  if (fd != -1)
    {
      list->fd = fd;
      printf("There's %d client slot left\n", (socket->max_client - 1) - list->fd);
      printf("The new client fd is : %d, client max => %d\n", list->fd, socket->max_client);
      list = my_put_in_list(list);
      socket->fds++;
    }
}

void			check_event(t_list *save, t_socket *socket)
{
  struct timeval	tv;
  fd_set		readfds;
  t_list		*tmp = save;

  tv.tv_sec = 0;
  tv.tv_usec = 1;
  if (socket->fds > 4)
    {
      FD_ZERO(&readfds);
      while (tmp->next != NULL)
	{
	  FD_SET(tmp->fd, &readfds);
	  tmp = tmp->next;
	}
    }
  if (socket->fds > 4 && xselect(socket->fds_max, &readfds, NULL, NULL, &tv))
    {
      tmp = save;
      while (tmp->next != NULL)
	{
	  if (FD_ISSET(tmp->fd, &readfds))
	    get_cmd(tmp);
	  tmp = tmp->next;
	}
    }
}

void			check_disconnected(t_list **list)
{
  t_list		*tmp;

  tmp = *list;
  while (tmp->next != NULL)
    {
      if (tmp->disconnected == 1)
	{
	  printf("Client n°%d is disconnected.\n", tmp->fd);
	  my_delete_from_list(list, tmp->fd);	  
	}
      tmp = tmp->next;
    }
}
